<?php

namespace App\Http\Controllers\Front;

use Illuminate\Http\Request;
use App\Http\Controllers\ApiController;
use App\Models\UserAddress;
use DB;
use Validator;

class UserAddressController extends ApiController
{

    /**
     * 获取地址列表
     */
    public function index() 
    {
        $address = UserAddress::owner()->get();
        if ($address->isEmpty()) {
            return $this->failed('获取地址信息失败');
        } else {
            return $this->success($address);
        }
       
    }

    /**
     * 新增地址
     */
    public function create(Request $request) {
        return view('front.member.addressEdit', ['id' => 0, 'select' => $request->input('select')]);
    }

    /**
     * 保存新增
     */
    public function store(Request $request) 
    {
        $messages = [
            'contact_name.required' => '请填写联系人名称',
            'contact_tel.required' => '请填写联系人联系方式',
            'area.required' => '请填写地区',
            'area.address' => '请填写详细地址'
        ];

        $validator = Validator::make($request->all(), [
            'contact_name' => 'required',
            'contact_tel' => 'required',
            'area' => 'required',
            'address' => 'required'
        ], $messages);

        if ($validator->fails()) {
            return $this->failed($validator->errors()->first());
        }

        $is_default = UserAddress::owner()->default()->count() ? 0 : 1;

        $create =  UserAddress::create([
            'contact_name' => $request->input('contact_name'),
            'contact_tel' => $request->input('contact_tel'),
            'area' => $request->input('area'),
            'address' => $request->input('address'),
            'is_default' => $is_default
        ]);

        if ($create) {
            return $this->message('创建成功');
        } else {
            return $this->failed('创建失败');
        }
    }

    /**
     * 编辑
     */
    public function edit($id, Request $request) {
        return view('front.member.addressEdit', ['id' => $id, 'select' => $request->input('select')]);
    }

    /**
     * 更新收货地址
     */
    public function update(Request $request, $id) {
        $messages = [
            'contact_name.required' => '请填写联系人名称',
            'contact_tel.required' => '请填写联系人联系方式',
            'area.required' => '请填写地区',
            'area.address' => '请填写详细地址'
        ];

        $validator = Validator::make($request->all(), [
            'contact_name' => 'required',
            'contact_tel' => 'required',
            'area' => 'required',
            'address' => 'required'
        ], $messages);

        if ($validator->fails()) {
            return $this->failed($validator->errors()->first());
        }

        $create =  UserAddress::owner()->where('id', $id)->update([
            'contact_name' => $request->input('contact_name'),
            'contact_tel' => $request->input('contact_tel'),
            'area' => $request->input('area'),
            'address' => $request->input('address')
        ]);

        if ($create) {
            return $this->message('更新成功');
        } else {
            return $this->failed('更新失败');
        }
    }

    /**
     * 获取指定地址
     */
    public function show($id)
    {
        $orm = UserAddress::owner();

        if ($id === 'default') {
            // 默认地址
            $address = $orm->default()->first();
        } else {
            $address = $orm->find($id);
        }

        if (!$address) {
            return $this->failed('获取地址信息失败');
        } else {
            return $this->success($address);
        }
    }

    /**
     * 设置默认地址
     */
    public function setDefault(Request $request) {
        $id = $request->input('id');
        $address = UserAddress::owner()->find($id);

        DB::beginTransaction();

        $resetAll =  UserAddress::owner()->where('id', '<>', $id)->update(['is_default' => 0]); // 重置该用户全部地址为不默认状态

        $address->is_default = 1;
        $setDefault = $address->save();
        
        if($resetAll && $setDefault) {
            DB::commit();
            return $this->success('设置成功');
        } else {
            DB::rollBack();
            return $this->failed('设置失败');
        }
    }

    /**
     * 删除地址
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        UserAddress::find($id)->delete();
        return $this->message('删除成功');
    }
}
